<?php
namespace Admin\Controller;

use Think\Controller;

class SucListController extends CommonController
{
    /*
	 * 方法: Index
	 * 功能：文章分类列表
	 * 传参：article_parent_id，一级分类ID
	**/
    public function index()
    {
        $ptitle='成功案例';
        $title='首页';
        $row = 10;
        $page = $_GET['p'];
        if ($page == '') {
            $page = 1;
        }
        //接受所传入的一级分类
//        $article_parent_id = I('article_parent_id');
        $article_parent_id = 1;
        //获取一级分类下所有的二级文章分类
        $where = array(
            'ac.status' => 1,
            'ac.article_parent_id' => $article_parent_id
        );
        $article_cate_list = M('article_cate ac')
            ->field('ac.*, ac2.article_cate_name as article_parent_name')
            ->join('__ARTICLE_CATE__ ac2 ON ac.article_parent_id = ac2.id')
            ->where($where)
            ->page($page, $row)
            ->order('id desc')
            ->select();
        $article_cate_num =  M('article_cate ac')
            ->join('__ARTICLE_CATE__ ac2 ON ac.id = ac2.article_parent_id')
            ->where($where)
            ->count();
        $Page = new \Think\Page($article_cate_num, $row);
        $show = $Page->show();
        $this->assign('ptitle', $ptitle);
        $this->assign('title', $title);
        $this->assign('page', $show);
        $this->assign('article_cate_list', $article_cate_list);
        $this->display();
    }

    /*
    * 方法: add
    * 功能：文章分类的添加
   **/
    public function add()
    {
        if(!IS_POST){
            $ptitle='成功案例';
            $title='添加';
            //查询所有一级分类
            $oneCate = M('article_cate')->where(array('article_parent_id' => 0))->select();
            $this->assign('ptitle', $ptitle);
            $this->assign('title', $title);
            $this->assign('oneCate', $oneCate);
            $this->display();
        }else{
            $data['article_parent_id'] = I('article_parent_id');
            $data['article_cate_name'] = I('article_cate_name');
            $data['status'] = I('status');
            $data['order'] = I('order');
            $data['add_time'] = date('Y-m-d H:i:s',time());
            $addResult = M('article_cate')->add($data);
            if($addResult){
                $this ->ajaxReturn(array('info'=>'添加成功！','status'=>1));
            }else{
                $this ->ajaxReturn(array('info'=>'添加失败！','status'=>0));
            }
        }
    }

    /*
    * 方法: edit
    * 功能：文章分类的修改
    * 参数：id，文章分类ID
   **/
    public function edit()
    {
        if(!IS_POST){
            $ptitle = '成功案例';
            $title = '修改';
            $id = I('id');
            //查询该文章分类信息
            $cateInfo = M('article_cate ac')
                ->field('ac.*, ac2.article_cate_name as article_parent_name')
                ->join('__ARTICLE_CATE__ ac2 ON ac.article_parent_id = ac2.id')
                ->where(array('ac.id' => $id))
                ->find();
            //查询所有一级分类
            $oneCate = M('article_cate')->where(array('article_parent_id' => 0))->select();
            $this->assign('oneCate', $oneCate);
            $this->assign('ptitle', $ptitle);
            $this->assign('title', $title);
            $this->assign('cateInfo', $cateInfo);
            $this->assign('id', $id);
            $this->display();
        }else{
            $data['id'] = I('id');
            $data['article_parent_id'] = I('article_parent_id');
            $data['article_cate_name'] = I('article_cate_name');
            $data['status'] = I('status');
            $data['order'] = I('order');
            $data['add_time'] = date('Y-m-d H:i:s',time());
            $editResult = M('article_cate')->save($data);
            if($editResult !== false){
                $this ->ajaxReturn(array('info'=>'修改成功！','status'=>1));
            }else{
                $this ->ajaxReturn(array('info'=>'修改失败！','status'=>0));
            }
        }
    }

    /*
   * 方法: edit
   * 功能：文章分类的删除
   * 参数：id，文章分类ID
  **/
    public function del()
    {
        $ids = I('ids');

        $map = array(
            'id' => array(
                'in',"$ids"
        ));
        if (M('article_cate')->where($map)->delete() !== false) {
            $this->success('恭喜，文章分类删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    /**
     * 文章列表
     */
    public function article_list(){
        $cate_id = I('ids');
        $list = M('article a')->field('ac.article_cate_name,a.*')
            ->join('__ARTICLE_CATE__ ac on ac.id=a.article_cate_id')
            ->where('article_cate_id='.$cate_id)->select();
        $this->assign('list',$list);
        $this->assign('cate_id',$cate_id);
        $this->display();
    }

    /**
     * 添加文章
     */
    public function article_add(){
        if(IS_POST){
            $data['article_cate_id'] = I('article_cate_id');
            $data['title'] = I('title');
            $data['content'] = I('content');
            $data['picture'] = I('picture');
            $data['detail'] = I('detail');
            $data['add_time'] = date('Y-m-d H:i:s',time());
            if(M('article')->add($data)){
                echo 1;
            }else{
                echo 0;
            }
            exit;
        }
        $cate_id = I('cate_id');
        $cate_name = M('article_cate')->field('id,article_cate_name')->where('id='.$cate_id)->find();
        $this->assign('cate',$cate_name);
        $this->display();
    }

    /**
     * 修改文章
     */
    public function article_edit(){
        if(IS_POST){
            $id = I('ids');
            $data['article_cate_id'] = I('article_cate_id');
            $data['title'] = I('title');
            $data['content'] = I('content');
            $data['picture'] = I('picture');
            $data['detail'] = I('detail');
            if(false == M('article')->where('id='.$id)->save($data)){
                echo 0;
            }else{
                echo 1;   //修改成功
            }
            exit;
        }
        $id = I('id');
        $cate_name = I('cate');
        $info = M('article')->where('id='.$id)->find();
        $this->assign('cate_name',$cate_name);
        $this->assign('info',$info);
        $this->display();
    }
    
    /**
     * 删除文章
     */
    public function article_del(){
        $id = I('id');
        $cate_id = I('cate_id');
        if(M('article')->where('id='.$id)->delete()){
            $url = U('SucList/article_list').'?ids='.$cate_id;
            header("Location:$url");
        }else{
            $this->error('删除失败');
        }

    }
}